package com.yanqu.road.server.cmd.coldRoom;

import com.yanqu.road.entity.player.UserColdRoomSite;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.ColdRoomPb;
import com.yanqu.road.pb.coldRoom.ColdRoomProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.CmdApi;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.ColdRoomModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;

@CmdApi(reqMsg = ColdRoomProto.ExtendSiteReqMsg.class, respMsg = ColdRoomProto.ExtendSiteRespMsg.class)
@Cmd(code = Protocol.S_COLDROOM_EXTEND_SITE, desc = "扩建坑位")
public class ExtendSiteCmd extends AbstractCommand {

    private int siteIndex;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        int siteNum = player.getModule(ColdRoomModule.class).getSiteNum();
        if (siteNum >= GameConfig.COLDROOM_MAX_SITE) {
            ret = GameErrorCode.E_COLDROOM_MAX_SITE;
            return;
        }
        siteIndex = siteNum + 1;
        ret = player.getModule(ColdRoomModule.class).extendSite(siteIndex);
    }

    @Override
    public void sendData(GamePlayer player) {
        ColdRoomProto.ExtendSiteRespMsg.Builder respMsg = ColdRoomProto.ExtendSiteRespMsg.newBuilder();
        respMsg.setRet(ret);
        if(0 == ret){
            List<Integer> judgmentList = StringUtils.stringToIntegerList(GameConfig.COLDROOM_LOOMS_INCREASE_JUDGMENT, "\\|");
            if(siteIndex > judgmentList.get(0)){
                int num = judgmentList.get(1);
                int start = siteIndex;
                for (int i = 0; i < num; i++) {
                    UserColdRoomSite site = player.getModule(ColdRoomModule.class).getUserColdRoomSite(start);
                    if(site != null) {
                        respMsg.addAddSite(ColdRoomPb.parseColdRoomSiteTempMsg(site));
                    }
                    start++;
                }
            }else {
                UserColdRoomSite site = player.getModule(ColdRoomModule.class).getUserColdRoomSite(siteIndex);
                respMsg.addAddSite(ColdRoomPb.parseColdRoomSiteTempMsg(site));
            }
        }
        player.sendPacket(Protocol.U_COLDROOM_EXTEND_SITE, respMsg);
    }
}
